package com.ibs.hadoop.hbase.service.run;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.ibs.hadoop.hbase.bean.QueryBean;
import com.ibs.hadoop.hbase.bean.UseComplaintBaseClass;
import com.ibs.hadoop.hbase.console.UserComplaintException;
import com.ibs.hadoop.hbase.console.UserComplaintFlow;

/**
 * 用来管理任务的提交与获取数据
* 源文件名：ExecQueryManger.java
* 文件版本：1.0.0
* 创建作者：Think
* 创建日期：2016年10月31日
* 修改作者：Think
* 修改日期：2016年10月31日
* 文件描述：TODO
* 版权所有：Copyright 2016 zjhz, Inc. All Rights Reserved.
*/
public class ExecQueryManger {

    /**
     * 生成异步执行队列信息,并传递执行方法
    * @字段说明 execQueue
    */
    private ExecuteQuery<QueryBean, List<UseComplaintBaseClass>> execQueue = new QueryExecuteWaitCache<QueryBean, List<UseComplaintBaseClass>>(
            new ServiceQueryExec());

    /**
     * 用来进行执行查询的纯种池信息
    * @字段说明 excelPool
    */
    private ExecutorService excelPool = Executors.newFixedThreadPool(32);

    public Map<String, List<UseComplaintBaseClass>> submitQuery(final QueryBean query, List<String> flows)
            throws Exception {

        if (null != flows && !flows.isEmpty()) {

            // 检查key是否存在
            for (String flowItem : flows) {
                if (UserComplaintFlow.getInstance(flowItem) == null) {
                    throw new UserComplaintException("flow " + flowItem + "not exists !!");
                }
            }

            CountDownLatch queryOver = new CountDownLatch(flows.size());

            // 对象缓存信息
            Map<String, List<UseComplaintBaseClass>> resultCache = new ConcurrentHashMap<String, List<UseComplaintBaseClass>>();
            // 提交所以任务
            for (String flow : flows) {

                // 提交任务至队列中
                excelPool.submit(() -> {
                    try {
                        List<UseComplaintBaseClass> list = execQueue.execute(flow, query);

                        if(null == list)
                        {
                            list = new ArrayList<UseComplaintBaseClass>();
                        }

                            
                        resultCache.putIfAbsent(flow, list);

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        // 不论成功都必须解锁,减数
                        queryOver.countDown();
                    }

                });
            }

            queryOver.await();

            return resultCache;

        }

        return null;

    }

    public static void main(String[] args) {
        ExecQueryManger manaer = new ExecQueryManger();
        QueryBean bean = new QueryBean();

        bean.setImsi("imsi");

        List<String> flowList = new ArrayList<>();

        flowList.add("ATTACH");
        flowList.add("DETACH");

        Map<String, List<UseComplaintBaseClass>> map = null;
        try {
            map = manaer.submitQuery(bean, flowList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(map);
    }

}
